package main;

import jinyilw.swing.FontTools;
import jinyilw.swing.visuals.Rcomp.RFrame;
import jinyilw.swing.visuals.border.RTitledBorder;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

public class MainFrame extends RFrame
{

	public static void main(String[] args)
	{
//		try
//		{
//			UIManager.setLookAndFeel(new NimbusLookAndFeel());
//		} catch (Throwable e)
//		{
//			e.printStackTrace();
//		}
		MainFrame frame = new MainFrame("人员编制匹配率计算和模拟仿真程序");
		frame.setVisible(true);
	}

	public MainFrame(String title) throws HeadlessException
	{
		super(title);
		setSize(660, 320);
		setResizable(false);
//		setTitleHeight(40);
		setTitleColor(Color.green);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setContentPane(centerPanel());
	}

	private JPanel centerPanel()
	{
		JPanel panel = new JPanel();

		panel.setLayout(new GridLayout(1, 0));
		panel.add(calculatePanel());
		panel.add(dynamicCalculatePanel());
		panel.add(simulationCalculatePanel());
		return panel;
	}

	private JPanel calculatePanel()
	{
		JPanel panel = new JPanel();
		panel.setBorder(new RTitledBorder("静态匹配率", TitledBorder.CENTER,
				TitledBorder.ABOVE_TOP, FontTools.getCustomFont(18),Color.yellow));
		panel.setLayout(new GridLayout(0, 1,0,6));

		JTextArea textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		textArea.setFocusable(false);
		textArea.setBackground(new Color(218, 218, 218));
		textArea.setText("\n说明：使用静态数据运行，不包含等级工作时间和年龄信息\n");
		panel.add(textArea);

		JButton runDefaultButton = new JButton("运行模板数据");
		panel.add(runDefaultButton);
		runDefaultButton.addActionListener((ActionEvent e) ->
				StaticMain.main(null));

		JButton chooseButton = new JButton("运行自定义数据");
		panel.add(chooseButton);
		chooseButton.addActionListener((ActionEvent e) ->
		{
			File file = chooseNativeFile(this, "静态 - 选择按模板规范的表格数据", "xlsx");
			if (file != null)
				StaticMain.calculate(file);
		});
		return panel;
	}

	private JPanel dynamicCalculatePanel()
	{
		JPanel panel = new JPanel();
//		panel.setBorder(new TitledBorder(null, "动态", TitledBorder.CENTER,
//				TitledBorder.ABOVE_TOP, new Font(null, Font.BOLD, 18)));
		panel.setBorder(new RTitledBorder("动态匹配率", TitledBorder.CENTER,
				TitledBorder.ABOVE_TOP, FontTools.getCustomFont(18),Color.yellow));
		panel.setLayout(new GridLayout(0, 1,0,6));

		JTextArea textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		textArea.setFocusable(false);
		textArea.setBackground(new Color(218, 218, 218));
		textArea.setText("\n说明：使用动态数据运行，包含等级工作时间和年龄信息\n");
		panel.add(textArea);

		JButton runDefaultButton = new JButton("运行模板数据");
		panel.add(runDefaultButton);
		runDefaultButton.addActionListener((ActionEvent e) ->
				DynamicMain.main(null));

		JButton chooseButton = new JButton("运行自定义数据");
		panel.add(chooseButton);
		chooseButton.addActionListener((ActionEvent e) ->
		{
			File file = chooseNativeFile(this, "动态 - 选择按模板规范的表格数据", "xlsx");
			if (file != null)
				DynamicMain.calculate(file);
		});
		return panel;
	}

	private JPanel simulationCalculatePanel()
	{
		JPanel panel = new JPanel();
//		panel.setBorder(new TitledBorder(null, "仿真", TitledBorder.CENTER,
//				TitledBorder.ABOVE_TOP, new Font(null, Font.BOLD, 18)));
		panel.setBorder(new RTitledBorder("模拟仿真", TitledBorder.CENTER,
				TitledBorder.ABOVE_TOP, FontTools.getCustomFont(18),Color.yellow));
		panel.setLayout(new GridLayout(0, 1,0,6));

		JTextArea textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		textArea.setFocusable(false);
		textArea.setBackground(new Color(218, 218, 218));
		textArea.setText(
				"\n说明：使用包含等级工作时间和年龄信息的初始数据，仿真模拟岗位编制匹配率变化情况");
		panel.add(textArea);

		JButton runDefaultButton = new JButton("运行模板数据");
		panel.add(runDefaultButton);
		runDefaultButton.addActionListener((ActionEvent e) ->
				SimulationMain.main(null));

		JButton chooseButton = new JButton("运行自定义数据");
		panel.add(chooseButton);
		chooseButton.addActionListener((ActionEvent e) ->
		{
			File file = chooseNativeFile(this, "仿真 - 选择按模板规范的表格数据", "xlsx");
			if (file != null)
				SimulationMain.calculate(file);
		});
		return panel;
	}

	public static void openFile(File file)
	{
		if (file == null || !file.exists())
			return;
		Desktop desktop = Desktop.getDesktop();
		try
		{
			desktop.open(file);
		} catch (IOException e)
		{
			e.printStackTrace();
			try
			{
				desktop.open(file.getParentFile());
			} catch (IOException e1)
			{
				e1.printStackTrace();
			}
		}
	}

	public static String getFileExt(String filename)
	{
		if ((filename != null) && (filename.length() > 1))
		{
			int dot = filename.lastIndexOf('.') + 1;
			if (dot == 0 || dot == filename.length())
				return null;
			return filename.substring(dot);
		}
		return null;
	}

	public static File chooseNativeFile(Component parent, String title,
			final String ext)
	{
		return getNativeFile(parent, title, FileDialog.LOAD, ext);
	}

	public static File getNativeFile(Component parent, String title, int mode,
			String... exts)
	{
		FileDialog fileDialog = createNativeFileDialog(parent, title, mode,
				exts);
		fileDialog.setVisible(true);
		File[] files = fileDialog.getFiles();
		if (files != null && files.length > 0)
			return files[0];
		return null;
	}
	public static FileDialog createNativeFileDialog(Component parent,
			String title, int mode, final String... exts)
	{
		FileDialog fileDialog = new FileDialog(getFrame(parent), title, mode);
		if (exts.length > 0)
		{
			fileDialog.setFilenameFilter((dir, name) -> {
				String fileExt = getFileExt(name);
				if (fileExt == null)
					return false;
				for (String ext : exts)
					if (fileExt.equalsIgnoreCase(ext))
						return true;
				return false;
			});
		}
		return fileDialog;
	}

	private static Frame getFrame(Component c)
	{
		for (Component p = c; p != null; p = p.getParent())
			if (p instanceof Frame)
				return (Frame) p;
		return null;
	}
}
